const {
  baseUrl
} = require('../nev').prod
//封装ajax
const vipUrl = '专属域名'
module.exports = {
  request: function (url, method = "GET", data = {}, isSubDomain = true) {
    // console.log('审查',url,method,data,isSubDomain=true)
    let fullUrl = `${baseUrl}/${url}`;
    //数据请求成功前的loading加载
    wx.showLoading({
      title: '数据加载中',
    })
    //promise封装request
    return new Promise((resolve, reject) => {
      wx.request({
        url: fullUrl,
        method,
        data,
        header: {
          //  'Content-type':'application/json'
          //  'Content-type':'application/x-www-form-urlencoded'
          'Content-type': 'application/json;charset=UTF-8',
          'x-app': 'a_1z41svq3kx6',
          'Authorization':wx.getStorageSync('userInfoData')?wx.getStorageSync('userInfoData').token : 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjQsInJvbGVOYW1lIjoiYWRtaW4iLCJpYXQiOjE3NDQxODgwODcsImV4cCI6MzMzMDE3ODgwODd9.xlliGiN4aDjTnnGbdrYwS257EpDFHiDHeJ2Z6C3MGRE'
          // 'Authorization':'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjQsImlhdCI6MTc0Mjc4NjU0OSwiZXhwIjoxNzQzMzkxMzQ5fQ.BpmVj_NaNAJDJhEYMwDRk6GSMVPh5T04ZKiQMDhAPu4'
        },
        success(res) {
          resolve(res.data)
          wx.hideLoading()
          //数据请求成功判断
          // if (res.statusCode === 200 && res.data.statue === 0) {
          //   resolve(res.data.data)
          //   wx.hideLoading()
          // } else {
          //   wx.showToast({
          //     title: res.data.message,
          //     icon: 'none'
          //   })
          //   reject(res.data.message)
          // }
        },
        fail(error) {
          // 添加错误日志输出
          console.error('接口请求失败详情:', {
            errMsg: error.errMsg,
            statusCode: error.statusCode,
            data: error.data
          })

          // 细化错误提示
          const errorMessage = error.errMsg.includes('timeout') ?
            '请求超时，请检查网络' :
            error.statusCode ?
            `服务器错误(${error.statusCode})` :
            '网络连接异常'

          wx.showToast({
            title: errorMessage,
            icon: 'none',
            duration: 3000
          })

          // 传递完整错误对象
          reject({
            code: error.statusCode || 500,
            message: errorMessage,
            raw: error
          })
        }
      })
    })
  }
}